<?php
/**
 *  +----------------------------------------------------------------------
 *  | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 *  +----------------------------------------------------------------------
 *  | Author: CRMEB Team <admin@crmeb.com>
 *  +----------------------------------------------------------------------
 */

/**
 * 帖子
 * @author crud自动生成代码
 * @date 2025/07/11 22:10:24
 */

namespace app\model\crud;


use crmeb\basic\BaseModel;

/**
 * Class Posts
 * @date 2025/07/11
 * @package app\model\crud
 */
class Posts extends BaseModel
{

    /**
     * 表名
     * @var string
     */
    protected $name = 'posts';

    /**
     * 主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 图片获取器
     * @date 2025-07-11
     * @param string $value
     * @return string
     */
    public function getImagesLabelAttr($value)
    {
        $value = $value ? json_decode($value, true) : [];
        return $value;
    }

    /**
     * 帖子状态获取器
     * @date 2025-07-11
     * @param string $value
     * @return string
     */
    public function getStatusLabelAttr($value)
    {
        $attr = '';
        switch ((int)$value){
            case 0:
                $attr = '待审核';
                break;
            case 1:
                $attr = '已审核';
                break;
            case 2:
                $attr = '封禁';
                break;
            case 3:
                $attr = '锁定';
                break;
        }
        return $attr;
    }


    /**
     * 发帖人一对一关联
     * @date 2025/07/11
     * @return \think\model\relation\HasOne
     */
    public function userIdHasOne()
    {
        return $this->hasOne(\app\model\user\User::class, 'uid', 'user_id')->bind(['user_name' => 'real_name', 'avatar', 'is_promoter']);
    }

    /**
     * 所在院校一对一关联
     * @date 2025/07/11
     * @return \think\model\relation\HasOne
     */
    public function universitIdHasOne()
    {
        return $this->hasOne(\app\model\crud\Universit::class, 'id', 'universit_id')->bind(['universit_name']);
    }

    public function postsComment(){
        return $this->hasMany(\app\model\crud\PostsComment::class, 'posts_id', 'id');
    }
}
